package com.student.service.student;


import com.student.dao.StudentDao;
import com.student.dao.impl.StudentDaoImpl;
import com.student.pojo.LimitPage;
import com.student.pojo.Student;

import java.util.List;

/**
 * @author lin945
 * @date 2020/4/27 21:04
 */
public class StudentService {
    private StudentDao studentDao = new StudentDaoImpl();

    public Student getStudentById(String id){
    return studentDao.queryStudentById(id);
}
    public boolean saveStudent(Student student) {
        if (student == null|studentDao.existStudent(student)) {
            return false;
        }
        return studentDao.saveStudent(student);
    }

    public LimitPage getLimitPageByName(int pageNum, int pageSize,String name){
        LimitPage limitPage=new LimitPage();
        List<Student> students = studentDao.queryStudentByName(name);
        int startIndex=(pageNum-1)*pageSize;
        //设置当前页
        limitPage.setNum(pageNum);
        int studentCount =students.size();
        //设置总数
        limitPage.setTotal(studentCount);
        //状况
        limitPage.setCode("ok");
        if(startIndex>studentCount){
            limitPage.setCode("err");
        }
        //设置是否有下一页
        limitPage.setHas_next(1);
        if(startIndex+pageSize>studentCount){
            limitPage.setHas_next(0);
        }

        int totalPage=studentCount/pageSize;
        if(studentCount%pageSize>0){
            totalPage++;
        }
        //总页数
        limitPage.setTotalPage(totalPage);

        //设置list
        limitPage.setRows(studentDao.queryStudentByName(startIndex,pageSize,name));
        return limitPage;
    }

    public LimitPage getLimitPage(int pageNum, int pageSize){
        LimitPage limitPage=new LimitPage();
        int startIndex=(pageNum-1)*pageSize;
        //设置当前页
        limitPage.setNum(pageNum);
        int studentCount = (int)studentDao.getStudentCount();
        //设置总数
        limitPage.setTotal(studentCount);
        //状况
        limitPage.setCode("ok");
        if(startIndex>studentCount){
            limitPage.setCode("err");
        }
        //设置是否有下一页
        limitPage.setHas_next(1);
        if(startIndex+pageSize>studentCount){
            limitPage.setHas_next(0);
        }

        int totalPage=studentCount/pageSize;
        if(studentCount%pageSize>0){
            totalPage++;
        }
        //总页数
        limitPage.setTotalPage(totalPage);

        //设置list
       limitPage.setRows(studentDao.querStudentlimtList(startIndex, pageSize));
        return limitPage;
    }


    List<Student> getStudentByName(String name){
        if(name == null || name.length() == 0){
            return null;
        }
        return studentDao.queryStudentByName(name);
    }
    public boolean updateStudent(Student student){
        return studentDao.updateStudent(student);
    }

    public boolean deleteStudent(Student student) {
        if(student.getId()==null){
            return false;
        }
        return studentDao.deleteStudent(student);
    }
}
